import React, { Component } from 'react'
import {add,editor,getlist} from './api/index'
export default class Open extends Component {
    state={
        name:'',
        pwd:""
    }
async componentDidMount(){
    //  console.log(this.props.location.state)
        let res=await getlist()
        // let {match:{params:{id}}}=this.props
        let {location:{state:{id}}}=this.props
        if(id){
            let {name,pwd}=res.data.res.find(item=>item.id==id)
        this.setState({
            name,pwd
        })
        }
    }
    render() {
        let {location:{state:{id}}}=this.props
        // console.log(this.props)
        let {name,pwd}=this.state
        return (
            <div>
                <div><input type="text" placeholder="请输入姓名" name="name" value={name} onChange={this.changevalue}/></div>
                <div><input type="password" placeholder="请输入内容" name="pwd" value ={pwd} onChange={this.changevalue}/></div>
                <button onClick={()=>this.ok(name,pwd,id)}>完成</button>
                {/* <button onClick={async ()=>id?await editor({name,pwd,id}):await add({name,pwd})}>完成</button> */}
            </div>
        )
    }
    changevalue=(e)=>{
        this.setState({
            [e.target.name]:e.target.value
        })
    }
    ok=async (name,pwd,id)=>{
        id?await editor({name,pwd,id}):await add({name,pwd});
        this.props.history.goBack()
    }
}
